# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openshadinglanguage

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
_ver_base=1.5
pkgver=1.5.1312.606e29a
pkgrel=1
pkgdesc="Advanced shading language for production GI renderers (mingw-w64)"
arch=('any')
url="https://github.com/imageworks/OpenShadingLanguage/"
license=("custom")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
depends=("${MINGW_PACKAGE_PREFIX}-openexr"
        "${MINGW_PACKAGE_PREFIX}-boost"
        "${MINGW_PACKAGE_PREFIX}-libpng" 
        "${MINGW_PACKAGE_PREFIX}-glew"
        "${MINGW_PACKAGE_PREFIX}-libtiff"
        "${MINGW_PACKAGE_PREFIX}-openimageio"
        "${MINGW_PACKAGE_PREFIX}-freetype"
        #"${MINGW_PACKAGE_PREFIX}-intel-tbb"
        "${MINGW_PACKAGE_PREFIX}-ilmbase"
        "${MINGW_PACKAGE_PREFIX}-openexr"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-clang=3.4" "${MINGW_PACKAGE_PREFIX}-gcc")
source=("${_realname}"::"git+https://github.com/imageworks/OpenShadingLanguage.git"
        0001-mingw-w64-fix.patch
        0002-dont-add-flex-include-dir.patch
        0003-fix-OIIO-windows.patch
        0005-Fix-cast-from-void-ptr.patch
        0006-Fix-weird-array-out-of-bounds-error.patch
        0007-Fix-bad-casts-with-intptr_t.patch
        0008-Install-dlls.patch
        0009-Fix-shader_is_defined.patch)
md5sums=('SKIP'
         'c225e66cd0a59a674b331b26374ba082'
         '78526dd22cea1d42238609a9cdfef816'
         'efc1d82cae57918af7df71af70d3d9b6'
         '73851a8ec414f099fe1195ea18d756f9'
         '33d03ef6eb6433c0df111098a6c7b177'
         '85ed91acdd3bd974a18b8d1c9ae3e79b'
         '7816fd1cd6d2fcbb2547384aa14c7760'
         '0d24b7695095edcb16612b8c0a36834f')

pkgver() {
  cd "$srcdir/$_realname"
  printf "%s.%s.%s" "$_ver_base" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${srcdir}/$_realname
  patch -p1 -i ${srcdir}/0001-mingw-w64-fix.patch
  patch -p1 -i ${srcdir}/0002-dont-add-flex-include-dir.patch
  patch -p1 -i ${srcdir}/0003-fix-OIIO-windows.patch
  patch -p1 -i ${srcdir}/0005-Fix-cast-from-void-ptr.patch
  patch -p1 -i ${srcdir}/0006-Fix-weird-array-out-of-bounds-error.patch
  patch -p1 -i ${srcdir}/0007-Fix-bad-casts-with-intptr_t.patch
  patch -p1 -i ${srcdir}/0008-Install-dlls.patch
  patch -p1 -i ${srcdir}/0009-Fix-shader_is_defined.patch
}

build() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null

  pushd ${MINGW_PREFIX} > /dev/null
  export PREFIX_DEPS=`pwd -W`
  popd > /dev/null

  export OPENIMAGEIOHOME=${MINGW_PREFIX}
  export LLVM_DIRECTORY=${MINGW_PREFIX}
  cd $_realname/src
  [[ -d build-${MINGW_CHOST} ]] && rm -r build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST} && cd build-${MINGW_CHOST}
  # DEXTRA_OSLEXEC_LIBRARIES is from "llvm-config --ldflags", OSL uses 
  # "llvm-config --libfiles" but that doesn't include all of them.
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${PREFIX_WIN} \
    -DILMBASE_CUSTOM=True \
    -DILMBASE_CUSTOM_INCLUDE_DIR=${PREFIX_DEPS}/include \
    -DILMBASE_CUSTOM_LIB_DIR=${PREFIX_DEPS}/lib \
    -DILMBASE_CUSTOM_LIBRARIES="Half Iex Imath IlmThread" \
    -DEXTRA_OSLEXEC_LIBRARIES="-lz -lffi -lncurses -lshell32 -lpsapi -limagehlp -lm" \
    -DLLVM_STATIC=1 \
    -DCMAKE_CXX_FLAGS="-DTINYFORMAT_ALLOW_WCHAR_STRINGS -Wno-error=unused-but-set-variable -std=gnu++11" \
    ../..
    #-DVERBOSE=1
  # $PWD/src/liboslcomp PATH addition needed for running oslc.exe during the build (for liboslcomp.dll)
  # ${MINGW_PREFIX}/bin PATH addition needed for finding libboost_system-mt.dll when running oslc.exe. 
  #PATH=$PWD/src/liboslcomp:${MINGW_PREFIX}/bin:"${PATH}" make VERBOSE=1
  PATH="$PWD/src/liboslcomp:${PATH}" make
}

package() {
  cd $_realname/src/build-${MINGW_CHOST}

  make install

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/OSL/
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/licenses/$pkgname
  mv "${pkgdir}${MINGW_PREFIX}"/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/$pkgname # TODO: Tell upstream about this shit
  mv "${pkgdir}${MINGW_PREFIX}"/{CHANGES,README.md,INSTALL} "${pkgdir}${MINGW_PREFIX}"/share/OSL/
  mv "${pkgdir}${MINGW_PREFIX}"/doc "${pkgdir}${MINGW_PREFIX}"/share/OSL/doc
  #mv "${pkgdir}${MINGW_PREFIX}"/shaders "${pkgdir}${MINGW_PREFIX}"/share/OSL/shadersE
}
